import * as Cesium from "cesium";

export default class PointPrimitiveManage {

  constructor(options) {
    this.pointCollection = new Cesium.PointPrimitiveCollection({
      blendOption:Cesium.BlendOption.OPAQUE,
      ...options
    })
    this._collection = new Cesium.AssociativeArray()
  }

  add(pointParams){
    const {id} = pointParams;

    const renderId = Cesium.defined(id) ? id : Cesium.createGuid()

    const point = this.pointCollection.add({...pointParams});

    point.id = point;    // 这里的ID是为了Cesium拾取到的时候使用
    point.renderId = renderId;

    this._collection.set(renderId,point)

    return point;
  }



  contains(pointPrimitive){
    return this.pointCollection.contains(pointPrimitive);
  }

  // todo 有bug
  destroy(){
    this._collection.removeAll()
    this.pointCollection.destroy();
  }

  get(index){
    return this.pointCollection.get(index);
  }

  getById(key){
    return this._collection.get(key)
  }

  isDestroyed(){
    return this.pointCollection.isDestroyed();
  }

  remove(pointPrimitive){
    const findIndex = this._collection.values.findIndex(val => val === pointPrimitive);

    if(findIndex === -1){
      console.warn(`PointPrimitiveManager.js Collection not find ：`,pointPrimitive);
    }else{
      const resultId = this._collection.values[findIndex].renderId;
      this._collection.remove(resultId)
    }
    return this.pointCollection.remove(pointPrimitive)
  }

  removeById(key){
    const point = this._collection.get(key)
    return  this.remove(point)
  }


  removeAll(){
    this._collection.removeAll()
    this.pointCollection.removeAll();
  }


  update(frame){
    if(this.isDestroyed()){
      return false
    }
    this.pointCollection.update(frame)
  }
}